package com.misspao.lottery.service;

import com.misspao.lottery.commen.ResultInfo;
import com.misspao.lottery.mapper.EmployeeMapper;
import com.misspao.lottery.mapper.PrizeMapper;
import com.misspao.lottery.model.Employee;
import com.misspao.lottery.model.EmployeeAndPrizeVO;
import com.misspao.lottery.model.Prize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>Title: </p>
 * <p>Company:misspao </p>
 *
 * @author: buwenmin
 * @Date: Create in 13:42 2018/1/30
 * @Description:
 */
@Service
public class LotteryService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PrizeMapper prizeMapper;
    public ResultInfo validatePhoneNum(String phonenum, String username) {
        Employee user = new Employee();
        user.setPhonenum(phonenum);
        user.setUsername(username);
      Employee employee =  employeeMapper.findUserByPhonenumAndUsername(user);
      if (employee ==null){
         return ResultInfo.fail("请输入正确的手机号和姓名");
      }else{
         return ResultInfo.success("手机号正确");
      }


    }
    @Transactional
    public ResultInfo getPrize(String phonenum) {
        Employee em =  employeeMapper.validateEmployeeByPhonenum(phonenum);
        if(em ==null) {
            return ResultInfo.fail("手机号无效");
        }
       if(em.getPrizeId() != 0){
           Prize id = prizeMapper.findById(em.getPrizeId());
           return ResultInfo.success(2,"您已经领过奖了",id);
       }

       //生成1-6的随机数,根据随机数查找奖品,如果奖品数量为0 继续随机,直到领取奖品成功
       synchronized(this){
          while (true){
              Random random = new Random();
              int prizeId =  (random.nextInt(89))+1;

              if(prizeId>=1 && prizeId<=82){
                  boolean b = updatePrizeId(1, phonenum);
                  if(b){
                      break;
                  }else{
                      continue;
                  }

              }
              if(prizeId>=83 &&prizeId<=85){
                  boolean b = updatePrizeId(2, phonenum);
                  if(b){
                      break;
                  }else{
                      continue;
                  }
              }
              if(prizeId==86 || prizeId==87){
                  boolean b = updatePrizeId(3, phonenum);
                  if(b){
                      break;
                  }else{
                      continue;
                  }
              }
              if(prizeId==88){
                  boolean b = updatePrizeId(4, phonenum);
                  if(b){
                      break;
                  }else{
                      continue;
                  }
              }
              if(prizeId == 89){
                  boolean b = updatePrizeId(5, phonenum);
                  if(b){
                      break;
                  }else{
                      continue;
                  }
              }

          }
          /* while(true){
               Random random = new Random();
               int prizeId =  (random.nextInt(5))+1;
               Prize prize = prizeMapper.findById(prizeId);
               if(prize.getNumber()!=0){
                   Employee employee = new Employee();
                   Prize p = new Prize();
                   employee.setPrizeId(prizeId);
                   employee.setPhonenum(phonenum);
                   p.setNumber(prize.getNumber()-1);
                   p.setId(prizeId);
                   prizeMapper.updatePrizeNumber(p);
                   employeeMapper.updateEmployeePrizeId(employee);
                   break;
               }
           }*/

       }

       //再次查询中奖纪录
        EmployeeAndPrizeVO prize = employeeMapper.findUserByPhonenum(phonenum);
       if(prize !=null){
           return ResultInfo.success(prize.getPrizeName());
       }else{
           return ResultInfo.fail("奖品已领完");
       }

    }
@Transactional
    public ResultInfo initPrize() {
        Prize p1 = new Prize();
        Prize p2 = new Prize();
        Prize p3 = new Prize();
        Prize p4 = new Prize();
        Prize p5 = new Prize();
        Prize p6 = new Prize();
       p1.setId(1);
       p1.setNumber(82);
       p2.setId(2);
       p2.setNumber(3);
       p3.setId(3);
       p3.setNumber(2);
       p4.setId(4);
       p4.setNumber(1);
       p5.setId(5);
       p5.setNumber(1);
       prizeMapper.updatePrizeNumber(p1);
       prizeMapper.updatePrizeNumber(p2);
       prizeMapper.updatePrizeNumber(p3);
       prizeMapper.updatePrizeNumber(p4);
       prizeMapper.updatePrizeNumber(p5);
        return ResultInfo.success("初始化奖品成功");
    }
    @Transactional
    public ResultInfo initEmployee(){
        employeeMapper.updatePrizeId();
        return ResultInfo.success("初始化员工成功");
    }

    public boolean updatePrizeId(int id,String phonenum ){
        boolean flag = false;
//        synchronized (this){
            Prize prize = prizeMapper.findById(id);
            if(prize.getNumber()!=0){
                Employee employee = new Employee();
                Prize p = new Prize();
                employee.setPrizeId(id);
                employee.setPhonenum(phonenum);
                p.setNumber(prize.getNumber()-1);
                System.out.println("***************************number: " + p.getNumber());
                p.setId(id);
                prizeMapper.updatePrizeNumber(p);
                employeeMapper.updateEmployeePrizeId(employee);
                flag = true;
           }

            return flag;
        //}

    }
}
